<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-row>
        <el-col :span="6" :xs="24">
          <el-form-item label="流水号" prop="serialNumber">
            <el-input v-model="queryParams.serialNumber" clearable
                      @keyup.enter="handleQuery" class="!w-220px"/>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="业务员" prop="personId">
            <el-select v-model="queryParams.personId" multiple clearable filterable
                       default-first-option collapse-tags collapse-tags-tooltip class="!w-220px">
              <el-option v-for="item in personList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="部门" prop="deptId">
            <el-select v-model="queryParams.deptId" multiple clearable filterable
                       default-first-option collapse-tags collapse-tags-tooltip class="!w-220px">
              <el-option v-for="item in deptList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="仓库" prop="warehouseId">
            <el-select v-model="queryParams.warehouseId" multiple clearable filterable
                       default-first-option collapse-tags collapse-tags-tooltip class="!w-220px">
              <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6" :xs="24">
          <el-form-item label="出库日期" prop="orderDate">
            <el-date-picker v-model="queryParams.orderDate" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                            start-placeholder="开始日期" end-placeholder="结束日期"
                            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px"/>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" clearable class="!w-220px">
              <el-option v-for="dict in getIntDictOptions(DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS)"
                         :key="dict.value" :label="dict.label" :value="dict.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="创建人" prop="creator">
            <el-select v-model="queryParams.creator" multiple clearable filterable
                       default-first-option collapse-tags collapse-tags-tooltip class="!w-220px">
              <el-option v-for="item in userList" :key="item.id" :label="item.nickname" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="创建时间" prop="createTime">
            <el-date-picker v-model="queryParams.createTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                            start-placeholder="开始日期" end-placeholder="结束日期"
                            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6" :xs="24">
          <el-form-item label="审核人" prop="auditor">
            <el-select v-model="queryParams.auditor" multiple clearable filterable
                       default-first-option collapse-tags collapse-tags-tooltip class="!w-220px">
              <el-option v-for="item in userList" :key="item.id" :label="item.nickname" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="审核时间" prop="auditTime">
            <el-date-picker v-model="queryParams.auditTime" value-format="YYYY-MM-DD HH:mm:ss" type="daterange"
                            start-placeholder="开始日期" end-placeholder="结束日期"
                            :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]" class="!w-220px"/>
          </el-form-item>
        </el-col>
        <el-col :span="12" :xs="24">
          <el-form-item>
            <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
            <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
            <el-button type="primary" plain @click="openForm('create')" v-hasPermi="['wms:rdrecord09:create']">
              <Icon icon="ep:plus" class="mr-5px" /> 新增
            </el-button>
            <el-button type="success" plain @click="handleExport" :loading="exportLoading" v-hasPermi="['wms:rdrecord09:export']">
              <Icon icon="ep:download" class="mr-5px" /> 导出
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" border :show-overflow-tooltip="true"
              style="height: calc(80vh - 50px - 50px - 50px - 35px)">
      <el-table-column label="流水号" align="center" prop="serialNumber" min-width="180" fixed="left" />
      <el-table-column label="业务员" align="center" prop="personName" width="110" />
      <el-table-column label="部门" align="center" prop="deptName" width="150" />
      <el-table-column label="仓库" align="center" prop="warehouseName" width="150" />
      <el-table-column label="出库日期" align="center" prop="orderDate" :formatter="dateFormatter2" width="120" />
      <el-table-column label="备注" align="center" prop="remark" width="200" />
      <el-table-column label="流程状态" align="center" prop="status" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.BPM_PROCESS_INSTANCE_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="creatorName" width="110" />
      <el-table-column label="创建时间" align="center" prop="createTime" :formatter="dateFormatter" width="180"/>
      <el-table-column label="审核人" align="center" prop="auditorName" width="110" />
      <el-table-column label="审核时间" align="center" prop="auditTime" :formatter="dateFormatter" width="180"/>
      <el-table-column label="操作" align="center" min-width="200" fixed="right">
        <template #default="scope">
          <el-button link type="info" @click="openForm('view', scope.row.id)" v-hasPermi="['wms:rdrecord09:query']">
            查看
          </el-button>
          <el-button link type="primary" v-show="scope.row.status === 0||scope.row.status === 3"
                     @click="openForm('update', scope.row.id)" v-hasPermi="['wms:rdrecord09:update']">
            编辑
          </el-button>
          <el-button link type="primary" v-show="scope.row.status === 0||scope.row.status === 3"
                     @click="submit(scope.row.id)" v-hasPermi="['wms:rdrecord09:update']">
            提交
          </el-button>
          <el-button link type="danger" v-show="scope.row.status === 0||scope.row.status === 3"
                     @click="handleDelete(scope.row.id)" v-hasPermi="['wms:rdrecord09:delete']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination :total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize" @pagination="getList"/>
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
<!--  <Rdrecord09Form ref="formRef" @success="getList" />-->
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { Rdrecord09Api, Rdrecord09VO } from '@/api/wms/rdrecord09'
import { useRouter} from 'vue-router'
import * as DeptApi from "@/api/system/dept";
import { PersonApi, PersonVO} from "@/api/wms/person";
import {WarehouseApi, WarehouseVO} from "@/api/wms/warehouse";
import * as UserApi from "@/api/system/user";
import bus from '@/utils/bus'

bus.on('sendMsg', (msg) => {
  if (msg === 'rdrecord09') {
    getList()
  }
})

/** 其他出库主 列表 */
defineOptions({ name: 'Rdrecord09' })
const { push } = useRouter() // 路由
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<Rdrecord09VO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  serialNumber: undefined,
  personId: undefined,
  deptId: undefined,
  warehouseId: undefined,
  orderDate: [],
  status: undefined,
  remark: undefined,
  createTime: [],
  auditor: undefined,
  auditTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const personList = ref<PersonVO[]>([])
const deptList = ref<DeptApi.DeptVO[]>([])
const warehouseList =  ref<WarehouseVO[]>([])
const userList =  ref<UserApi.UserVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await Rdrecord09Api.getRdrecord09Page(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (id) {
    push('/inventory/rdrecord09FormInfo?type=' + type + '&id=' + id)
  } else {
    push('/inventory/rdrecord09Form?type=' + type)
  }
  //formRef.value.open(type, id)
}

/** 提交按钮操作 */
const submit = async (id: number) => {
  try {
    await message.confirm('是否提交')
    await Rdrecord09Api.submit(id)
    message.success(t('common.submit'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await Rdrecord09Api.deleteRdrecord09(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await Rdrecord09Api.exportRdrecord09(queryParams)
    download.excel(data, '其他出库主.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(async () => {
  // 加载业务员
  personList.value = await PersonApi.getPersonSimpleList()
  // 加载部门
  deptList.value = await DeptApi.getSimpleDeptList()
  // 加载仓库
  warehouseList.value = await WarehouseApi.getWarehouseSimpleList()
  // 加载用户
  userList.value = await UserApi.getSimpleUserList()
  await getList()
})
</script>
